﻿var INIT_DATA = ["", "新窗口"];

//定义转向函数
function turnTo(url, text) {
    //debugger;
    Ext.get('mycontent').dom.src = "../"+url;
    document.title = text;
    /*
    function chagneTile() {
    }
    var dt = new Ext.util.DelayedTask();
    dt.delay(1000,chagneTile, this);*/
	function successFun(action) {
		//debugger;
	
	}
	function failureFun(action) {
	
	}
	Ext.Ajax.request({
	   url: '../DataDict?funcname=cururl',
	   method: 'POST', 	   
	   success: successFun,
	   failure: failureFun
	});

    //INIT_DATA[0] = url;
}


Ext.onReady(function() {
    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'north',
            contentEl: 'menubar',

            height: 30,
            bbar: toolbar
        }, {
            region: 'center',
            html: '<iframe id="mycontent" frameBorder="0" src="" width="100%" height="100%"></iframe>'
        }, {
            region: 'south',
            html: '<iframe id="south-iframe" SCROLLING="no" frameBorder="0" src="bottom.jsp"  width="100%" height="100%"></iframe>',
            height:26 
        }]
    });
    
    var toolbar = new Ext.Toolbar('menubar');
    
    var ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: 'menu.xml', method: 'POST'}),
		reader: new Ext.data.XmlReader({
            record: 'menu',
            id: 'id'
		}, [
            {name: 'id', mapping: 'id'},
            {name: 'text', mapping: 'text'},
            {name: 'parent', mapping: 'parent'},
            {name: 'hasChild', mapping: 'hasChild'},
            {name: 'url', mapping: 'url'}
		])
	});

	ds.on("load",
		 function(dataset) {
			var record = null;
            var hasSeparator = true;
			var tbmap = [];
			var menus = [];
			for(var i = 0; i < dataset.getTotalCount(); i++)
		    {
		        record = dataset.getAt(i);
		        id = record.get("id");
		        menus[id] = new Ext.menu.Menu({id: id});
		        tbmap[i] = record;
		    }
		 
		   Ext.each(tbmap,function(record,index,arLst)
		   {
		        id = record.get("id");
				text = record.get("text");
				parent1 = record.get("parent");						
				hasChild = record.get("hasChild");
				url = record.get("url");
				if(parent1 == "1") //一级菜单
				{
					if(hasChild == "true") {
						toolbar.add({id: id, text: text, menu:menus[id]});
						if(hasSeparator) {toolbar.add("-")}
					} else {
						toolbar.add({id: id, text: text, handler:turnTo.createDelegate(this,[url, text])});
						if(hasSeparator) {toolbar.add("-")}
					}
				} 
				else 
				{
					if(hasChild == "true") 
					{
					    if(menus[parent1])
						{
						    menus[parent1].add({id: id, text: text, menu: menus[id], icon: '../../support/images/bogus.png'});
						}
					} 
					else 
					{
					  if(menus[parent1])
						{
						    menus[parent1].add({id: id, text: text, icon: '../../support/images/bogus.png', handler: turnTo.createDelegate(this,[url, text])});
						}
					}
				}
		    
		   });

		}
	 );
	 
	 
		
	ds.load();
	
	//turnTo()
	if (INIT_DATA[0]) {
		//alert(INIT_DATA[0]);
		//turnTo(INIT_DATA[0], INIT_DATA[1]);
	} else {
		//debugger;
		turnTo("welcome.html", "空调供应链管理系统")
	}
});
